package com.lxmes.gs.controller;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.myWorkingHoursVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.MyWorkingHours;
import com.lxmes.gs.service.IMyWorkingHoursService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 我的工时Controller
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "我的工时")    
@RestController
@RequestMapping("/gs/myWorkingHours")
public class MyWorkingHoursController extends BaseController{
    @Autowired
    private IMyWorkingHoursService myWorkingHoursService;

    /**
     * 查询我的工时列表
     */
    @ApiOperation("查询我的工时列表")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyWorkingHoursSearchVO myWorkingHours){
        startPage();
        List<MyWorkingHours> list = myWorkingHoursService.selectMyWorkingHoursList(BeanUtil.copyProperties(myWorkingHours,MyWorkingHours.class));
        return getDataTable(Convert.convert(new TypeReference<List<MyWorkingHoursResultVO>>() {},list));
}

    /**
     * 导出我的工时列表
     */
    @ApiOperation("导出我的工时列表")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:export')")
    @Log(title = "我的工时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyWorkingHours myWorkingHours){
        List<MyWorkingHours> list = myWorkingHoursService.selectMyWorkingHoursList(myWorkingHours);
        List<MyWorkingHoursExportVO> exportList = Convert.convert(new TypeReference<List<MyWorkingHoursExportVO>>() {}, list);
        ExcelUtil<MyWorkingHoursExportVO> util = new ExcelUtil<MyWorkingHoursExportVO>(MyWorkingHoursExportVO.class);
        util.exportExcel(response, exportList, "我的工时数据");
    }

    /**
     * 获取我的工时详细信息
     */
    @ApiOperation("获取我的工时详细信息")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(myWorkingHoursService.selectMyWorkingHoursById(id));
    }

    /**
     * 新增我的工时
     */
    @ApiOperation("新增我的工时")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:add')")
    @Log(title = "我的工时", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyWorkingHoursCreateVO myWorkingHours){
        return toAjax(myWorkingHoursService.insertMyWorkingHours(BeanUtil.copyProperties(myWorkingHours,MyWorkingHours.class)));
    }

    /**
     * 修改我的工时
     */
    @ApiOperation("修改我的工时")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:edit')")
    @Log(title = "我的工时", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyWorkingHoursUpdateVO myWorkingHours){
        return toAjax(myWorkingHoursService.updateMyWorkingHours(BeanUtil.copyProperties(myWorkingHours,MyWorkingHours.class)));
    }

    /**
     * 删除我的工时
     */
    @ApiOperation("删除我的工时")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:remove')")
    @Log(title = "我的工时", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(myWorkingHoursService.deleteMyWorkingHoursByIds(ids));
    }

    /**
     * 我的工时提报审核
     */
    @ApiOperation("我的工时提报审核")
    @PreAuthorize("@ss.hasPermi('gs:myWorkingHours:audit')")
    @Log(title = "我的工时提报审核")
    @PostMapping("/auditMyWorkingHours/{ids}")
    public AjaxResult auditMyWorkingHours(@PathVariable Long[] ids){
        return toAjax(myWorkingHoursService.auditMyWorkingHours(ids));
    }
    /**
     * 导入我的工时
     */
    @Log(title = "我的工时", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<MyWorkingHoursImportVO> util = new ExcelUtil<>(MyWorkingHoursImportVO.class);
        List<MyWorkingHoursImportVO> myWorkingHoursList = util.importExcel(file.getInputStream());
        String message = myWorkingHoursService.imporIteminfo(Convert.convert(new TypeReference<List<MyWorkingHours>>() {},myWorkingHoursList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<MyWorkingHoursImportVO> util = new ExcelUtil<>(MyWorkingHoursImportVO.class);
        util.importTemplateExcel(response,"我的工时");
    }
}
